# -*- coding: utf-8 -*-
'''
Created on 2013-4-26

@author: dyq
'''
from Common.htmlHlep import Find
from Common.tool import tool
from Cms.plus import  page,user
from Models import  models
import  hashlib,datetime

@page.page_render(user.Admin())
def Index(request):
        '''
                            列表页
        '''
        return Find.View(request)
    
    
@page.page_render(user.Admin())
def List(request):
        '''
                            列表页
        '''
        IsOpen=int(request.POST.get("IsOpen",-1))
        State=int(request.POST.get("State",-1))
        ApplyUserName=request.POST.get("ApplyUserName","")
        
        list=models.Message_Board.objects.all()
        if IsOpen!=-1:
            list= list.filter(IsOpen=IsOpen)
            
        if State!=-1:
            list= list.filter(State=State)
            
        if len(ApplyUserName)!=0:
            list= list.filter(ApplyUserName__contains=ApplyUserName)       

        list=list.order_by("-ApplyTime") #倒序加上-号
        
        return Find.View(request,Find.paginator(request,list))

@page.page_render(user.Admin())
def View(request):
        '''
                            查看
        '''
        info =models.Message_Board.objects.get(MessageId=request.GET.get("MessageId"))
        return Find.View(request,info)

@page.page_render(user.Admin())
def Edit(request):
        '''
                            编辑
        '''
        info =models.Message_Board.objects.get(MessageId=request.GET.get("MessageId"))
        
        return Find.View(request,info)
    
@page.page_render(user.Admin())
def DoEdit(request):
        '''
                            编辑
        '''
        info=models.Message_Board.objects.get(MessageId=request.POST.get("MessageId"))
        Find.UpdateModel(request,info)
        userinfo=user.user().GetUser(request)
        info.ReplyUserId=userinfo.UserId
        info.ReplyUserName=userinfo.NickName
        info.ReplyTime=datetime.datetime.now()
        info.State=1
        info.save()
        
        user.user().Log(request,7, "编辑留言："+str(info.MessageId)+" State:"+str(info.State))
        
        return Find.JsonX(False, "操作成功")
    
@page.page_render(user.Admin())
def DoDel(request):
        '''
                            删除
        '''
        models.Message_Board.objects.get(MessageId=request.GET.get("MessageId")).delete()
        
        user.user().Log(request,7, "删除留言："+str(request.GET.get("MessageId")))
        
        return Find.JsonX(False, "操作成功")
        
@page.page_render(user.Admin())
def DoBatch(request):
        '''
                            批量操作
        '''
        
        dataOrderList=str(request.POST.get("MessageIdList")).split(',')
        IsOpen=int(request.POST.get("selectBatch",-1))
        for key in dataOrderList:
            info=models.Message_Board.objects.get(MessageId=key)
            if IsOpen!=-1:
                info.IsOpen=IsOpen
            info.State=1
            userinfo=user.user().GetUser(request)
            info.ReplyUserId=userinfo.UserId
            info.ReplyUserName=userinfo.NickName
            info.ReplyTime=datetime.datetime.now()
            
            info.save()
        user.user().Log(request,7, "批量操作IsOpen："+str(IsOpen))
        
        return Find.JsonX(False, "操作成功")

    
    